package com.ruoyi.sales.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.core.domain.entity.ZcStock;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sales.domain.ZcProduct;
import com.ruoyi.sales.domain.ZcStoreInfo;
import com.ruoyi.sales.service.IZcProductService;
import com.ruoyi.sales.service.IZcStockService;
import com.ruoyi.sales.service.IZcStoreInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.mapper.ZcStoreMaterialMapper;
import com.ruoyi.sales.domain.ZcStoreMaterial;
import com.ruoyi.sales.service.IZcStoreMaterialService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.transaction.annotation.Propagation.MANDATORY;

/**
 * 原料仓库Service业务层处理
 *
 * @author woo
 * @date 2024-04-08
 */
@Service
public class ZcStoreMaterialServiceImpl extends ServiceImpl<ZcStoreMaterialMapper, ZcStoreMaterial> implements IZcStoreMaterialService {
    @Autowired
    private ZcStoreMaterialMapper zcStoreMaterialMapper;
    @Autowired
    private IZcStoreInfoService storeInfoService;
    @Autowired
    private IZcStockService stockService;
    @Autowired
    private IZcProductService productService;
    /**
     * 查询原料仓库
     *
     * @param materialId 原料仓库主键
     * @return 原料仓库
     */
    @Override
    public ZcStoreMaterial selectZcStoreMaterialByMaterialId(Long materialId) {
        return zcStoreMaterialMapper.selectZcStoreMaterialByMaterialId(materialId);
    }

    /**
     * 查询原料仓库列表
     *
     * @param zcStoreMaterial 原料仓库
     * @return 原料仓库
     */
    @Override
    public List<ZcStoreMaterial> selectZcStoreMaterialList(ZcStoreMaterial zcStoreMaterial) {
        return zcStoreMaterialMapper.selectZcStoreMaterialList(zcStoreMaterial);
    }

    /**
     * 新增原料仓库
     *
     * @param zcStoreMaterial 原料仓库
     * @return 结果
     */
    @Override
    public int insertZcStoreMaterial(ZcStoreMaterial zcStoreMaterial) {
                zcStoreMaterial.setCreateTime(DateUtils.getNowDate());
                zcStoreMaterial.setDel((long) 0);
                //采购人
                zcStoreMaterial.setStaffId(SecurityUtils.getUserId());
                checkStoreStatus(zcStoreMaterial);
            return zcStoreMaterialMapper.insertZcStoreMaterial(zcStoreMaterial);
    }

    private void checkStoreStatus(ZcStoreMaterial zcStoreMaterial) {
        int storeCount = selectCountByStore(zcStoreMaterial.getStoreId());
        int materialCount = Integer.valueOf(zcStoreMaterial.getMaterialCount());
        ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreMaterial.getStoreId());
        int produceCount = (int)(zcStoreInfo.getLine()*zcStoreInfo.getWide());
        int count = produceCount - materialCount - storeCount;
        zcStoreMaterial.setLastCount(String.valueOf(count));
        if (count==0){
            zcStoreInfo.setStatus(String.valueOf(1));
            storeInfoService.updateZcStoreInfo(zcStoreInfo);
        }
    }

    /**
     * 修改原料仓库
     *
     * @param zcStoreMaterial 原料仓库
     * @return 结果
     */
    @Override
    public int updateZcStoreMaterial(ZcStoreMaterial zcStoreMaterial) {
                zcStoreMaterial.setUpdateTime(DateUtils.getNowDate());
        return zcStoreMaterialMapper.updateZcStoreMaterial(zcStoreMaterial);
    }

    /**
     * 批量删除原料仓库
     *
     * @param materialIds 需要删除的原料仓库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZcStoreMaterialByMaterialIds(Long[] materialIds) {
        if (materialIds == null || materialIds.length == 0) {
            // 如果materialIds为空或没有元素，直接返回0表示没有删除任何记录
            return 0;
        }
        for (Long materialId : materialIds) {
            ZcStoreMaterial material = zcStoreMaterialMapper.selectZcStoreMaterialByMaterialId(materialId);
            ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(material.getStoreId());
            zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()-Long.valueOf(material.getMaterialCount()));
            zcStoreInfo.setStatus("0");
            storeInfoService.updateZcStoreInfo(zcStoreInfo);
        }
        return zcStoreMaterialMapper.deleteZcStoreMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除原料仓库信息
     *
     * @param materialId 原料仓库主键
     * @return 结果
     */
    @Override
    public int deleteZcStoreMaterialByMaterialId(Long materialId) {
        return zcStoreMaterialMapper.deleteZcStoreMaterialByMaterialId(materialId);
    }

    @Override
    public int selectCountByStore(Long storeId) {
        QueryWrapper<ZcStoreMaterial> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("store_id",storeId).last("limit 1");
        List<ZcStoreMaterial> zcStoreMaterials = zcStoreMaterialMapper.selectList(qw);
        //计算当前仓库已经被用了多少仓储
        int materialCount=0;
        if (zcStoreMaterials.size()==0){
            return materialCount;
        }else if (zcStoreMaterials.size()!=0){
            for (ZcStoreMaterial material : zcStoreMaterials) {
                materialCount = Integer.valueOf(material.getMaterialCount());
                materialCount++;
            }
        }
        return materialCount;
    }

    @Override
//    @Transactional(propagation = Propagation.MANDATORY)
    public Long checkStoreMaterial(Long productNumber, Long lastCount) {
        ZcProduct zcProduct = productService.selectZcProductByProductId(productNumber);
        QueryWrapper<ZcStoreMaterial> qw = new QueryWrapper<>();
        qw.eq("del",0);
        List<ZcStoreMaterial> zcStoreMaterials = zcStoreMaterialMapper.selectList(qw);
        long l = lastCount * 2;
        for (ZcStoreMaterial zcStoreMaterial : zcStoreMaterials) {
            if (zcStoreMaterial.getStockId()==zcProduct.getStockId()){
                Long materialCount = Long.valueOf(zcStoreMaterial.getMaterialCount());
                ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreMaterial.getStoreId());
                if (l==0){
                    break;
                }else if (l<materialCount){
                    // 足够的原料
                    materialCount -= l;
                    zcStoreInfo.setUntrueCount(l);
                    zcStoreInfo.setTrueCount(materialCount);
                    l = 0;
                }else {
                    //原料不足
                    zcStoreInfo.setUntrueCount(l);
                    zcStoreInfo.setTrueCount(Long.valueOf(0));
                    l -= materialCount;
                }
//                else if (l>materialCount){
//                    l -= materialCount;
//                    zcStoreInfo.setUntrueCount(materialCount);
//                    zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()-materialCount);
//                }else {
//                    zcStoreInfo.setUntrueCount(l);
//                    zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()-l);
//                    l = materialCount - l;
//                }
                storeInfoService.updateZcStoreInfo(zcStoreInfo);
            }
        }
        //返回还需原料
        return l;
    }
}